#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using vec = vector<int>;
using vec2 = vector<vector<int>>;

class Main {
	int N, M;
	int ans = 0;
	vec grid;
	vec sit;
	vec sat;

    public:
	Main(int n, int m) : N(n), M(m), grid(n + 1, 0)
	{
		for (int i = 1; i <= N; i++) {
			string s;
			cin >> s;
			int status = 0;
			for (int j = 0; j < M; j++) {
				status |= (s[j] == 'H') ? (1 << j) : 0;
			}
			grid[i] = status;
		}

		for (int s = 0, ts = 0; s < (1 << M); s++, ts = s) {
			if ((s & (s >> 1)) || (s & (s >> 2))) {
				continue;
			}
			sit.push_back(s);

			int cnt = 0;
			while (ts) {
				cnt += (ts & 1);
				ts >>= 1;
			}

			sat.push_back(cnt);
		}
	}

	int max_nums()
	{
		vec2 dp(N + 1, vec(1 << M, 0));
		for (int i = 0; i < sit.size(); i++) {
			if (sit[i] & grid[1]) {
				continue;
			}
			dp[1][sit[i]] = sat[i];
		}

		for (int i = 2; i <= N; i++) {
			for (int cur = 0, cs = sit[cur]; cur < sit.size(); cur++, cs = sit[cur]) {
				if (cs & grid[i]) {
					continue;
				}
				for (int pre = 0, ps = sit[pre]; pre < sit.size(); pre++, ps = sit[pre]) {
					if (ps & grid[i - 1]) {
						continue;
					}
					for (int ppre = 0, pps = sit[ppre]; ppre < sit.size(); ppre++, pps = sit[ppre]) {
						if (pps & grid[i - 2]) {
							continue;
						}

						if ((cs & ps) || (cs & pps) || (ps & pps)) {
							continue;
						}

						dp[i][cs] = max(dp[i][cs], dp[i - 1][ps] + sat[cs]);
					}
				}
			}
		}

		return *max_element(dp[N].begin(), dp[N].end());
	}

	int max_num(int i, int ps, int pps, int val, vector<vec2> &dp)
	{
		if (i == N + 1) {
			return dp[i][ps][pps] = max(dp[i][ps][pps], val);
		}

		if (dp[i][ps][pps] != -1) {
			return dp[i][ps][pps];
		}

		for (int k = 0, cs = sit[k]; k < sit.size(); cs = sit[++k]) {
			if ((cs & pps) || (cs & ps) || (ps & pps) || (cs & grid[i])) {
				continue;
			}
			max_num(i + 1, cs, ps, val + sat[k], dp);
		}

		return dp[i][ps][pps];
	}
};

int main()
{
	int n, m;
	cin >> n >> m;
	Main app(n, m);
	//cout << app.max_nums() << endl;
	vector<vec2> dp(n + 2, vec2(1 << m, vec(1 << m, -1)));
	cout << app.max_num(1, 0, 0, 0, dp) << endl;
	return 0;
}